<?php
	/* Gérald Plusquellec 2008*/
	
	if (!is_dir(DIR_OPT))
		die('Error class option');
	
	class GP_options
	{
		static $_instance;
		public $_key;
		
		// singleton
		public static function &getInstance() 
		{
			if (!self::$_instance instanceof self) {
				self::$_instance = new self();
			}
            return self::$_instance;
		}
		
		function __construct()
        {
            if (null === session_id()) {
                $this->_key = md5($_SERVER['REMOTE_ADDR']);
            } else {
                $this->_key = session_id();
            }
        }
		
		function __destruct() 
		{
			return true;
		}
		
		function deloption($name, $id)
		{
			return $this->setoption($name, null, $id);
		}
		
		function setoption($name, $val, $id)
		{
			if (null === $id) {
				$id = $this->_key;
			} else {
				$id = md5($id);
			}
            $name = md5($name . $id);
			$dir = dossier_options($name);
			$file = DIR_OPT . $dir . $name . '.option';
			if(is_file($file)) {
				@unlink($file);
			}
            @touch($file);
			$val = serialize($val);
			file_put_contents($file, $val);
			return true;
		}
		
		function getoption($name, $id)
		{
			if (null === $id) {
				$id = $this->_key;
			} else {
				$id = md5($id);
			}
            $name = md5($name . $id);
			$dir = dossier_options($name);
			$file = DIR_OPT . $dir . $name . '.option';
			if(is_file($file)) {
				return unserialize(file_get_contents($file));
			}
            return null;
		}
		
		function record($name, $value)
		{
			$this->delete($name);
            $value = serialize($value);
			$this->$name = $value;
		}
		
		function get($name)
		{
			if (isset($this->$name)) {
                return unserialize($this->$name);
            } else {
                return null;
            }
		}
		
		function delete($name)
		{
			unset($this->$name);
			$this->$name = null;
		}
	}
?>